//
//  LGChangeNameViewController.m
//  LoverGo
//
//  Created by YeXiao on 14-5-22.
//  Copyright (c) 2014年 yexiao. All rights reserved.
//

#import "LGUtil.h"
#import "DataCenter.h"
#import "RequestManager.h"
#import "LGChangeViewController.h"

@interface LGChangeViewController ()

@end

@implementation LGChangeViewController

-(UITextField*)getTextField:(NSString*)titleStr
{
    UITextField *_tF = [[UITextField alloc] initWithFrame:CGRectMake(0, 55, 275, 30)];
    [_tF setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型
//    _tF.delegate = self;
    return _tF;
}

-(id)initWithType:(int)sitemType
{
    if ([self init]) {
        _type = sitemType;
        _tF1 = nil;
        _tF2 = nil;
        _tF3 = nil;
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"男",@"女",nil];
    _segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    UIView *view = [[UIView alloc] initWithFrame:[LGUtil getViewFrame]];
    view.backgroundColor = [UIColor whiteColor];
    UIButton *_loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200, 44)];
    _loginBtn.backgroundColor = [UIColor colorWithRed:102.0/255.0f green:221.0/255.0f blue:225.0/255.0f alpha:100];
    [_loginBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    [_loginBtn addTarget:self action:@selector(btnClieck:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_segmentedControl];
    _segmentedControl.hidden = YES;
    [view addSubview:_loginBtn];
    
    NSString *tStr;
    switch (_type) {
        case 0:
        {
            _segmentedControl.hidden = YES;
            _tF1 = [[UITextField alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 275)/2, 10, 275, 30)];
            [_tF1 setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型
            _tF1.delegate = self;
            [_tF1 becomeFirstResponder];
            [view addSubview:_tF1];
            _loginBtn.tag = 0;
            [self dinglan:@"修改用户名"];
            [_loginBtn setCenter:CGPointMake(SCREEN_WIDTH/2, 70)];
            break;
        }
        case 1:
        {
            _segmentedControl.hidden = NO;
            _segmentedControl.frame = CGRectMake(60.0, 10.0, 200, 40);
            _segmentedControl.selectedSegmentIndex = 1;
            _segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;;
            [_loginBtn setCenter:CGPointMake(SCREEN_WIDTH/2, 85)];
            _loginBtn.tag = 1;
            [self dinglan:@"修改性别"];
            break;
        }
        case 2:
        {
           _loginBtn.tag = 2;
            _segmentedControl.hidden = YES;
            _tF1 = [[UITextField alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 275)/2, 10, 275, 30)];
            [_tF1 setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型
            _tF1.delegate = self;
            _tF1.placeholder = @"请输入原密码";
            [_tF1 becomeFirstResponder];
            [view addSubview:_tF1];
            
            _tF2 = [[UITextField alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 275)/2, 45, 275, 30)];
            [_tF2 setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型
            _tF2.delegate = self;
            _tF2.placeholder = @"请输入新密码";
            [view addSubview:_tF2];
            
            _tF3 = [[UITextField alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 275)/2, 80, 275, 30)];
            [_tF3 setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型
            _tF3.delegate = self;
            _tF3.placeholder = @"请再输入新密码";
            [view addSubview:_tF3];
            
            [_loginBtn setCenter:CGPointMake(SCREEN_WIDTH/2, 140)];
            [self dinglan:@"修改密码"];
            break;
        }
            
        default:
            break;
    }
    
    
    [self setView:view];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeSucc:) name:userUpDateSPath object:nil];
}
-(void)dinglan:(NSString *)titlename{
    UIButton * backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,20, 20)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(dinglanBackBtn) forControlEvents:UIControlEventTouchUpInside] ;
    [backBtn setShowsTouchWhenHighlighted:YES] ;
    UIBarButtonItem *backbutton = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backbutton;
    title = [[UILabel alloc]initWithFrame:CGRectMake(90, 0, 140, 30)];
    title.textAlignment = UITextAlignmentCenter;
    title.textColor = LGCOLOR_blue;
    title.text = titlename;
    self.navigationItem.titleView = title;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"barBG.png"] forBarMetrics:UIBarMetricsDefault];
}
-(void)dinglanBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewDidUnload
{
    [[NSNotificationCenter defaultCenter] removeObserver:userUpDateSPath name:nil object:self];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (_tF1) {
        [_tF1 resignFirstResponder];
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{

    if (_type == 2) {
        if (textField == _tF1) {
            [_tF1 resignFirstResponder];
            [_tF2 becomeFirstResponder];
        }else if (textField == _tF2) {
            [_tF2 resignFirstResponder];
            [_tF3 becomeFirstResponder];
        }else if (textField == _tF3) {
            [_tF3 resignFirstResponder];
        }
    }else
    {
        if (textField == _tF1) {
            [_tF1 resignFirstResponder];
        }
    }
    return YES;
}
-(void)changeSucc:(NSNotification *)nof
{
    if ([[nof name] compare:userUpDateSPath] == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)btnClieck:(id)sender
{
    NSMutableDictionary *para = [[NSMutableDictionary alloc] init];
    [para setObject:[DataCenter getSingleton].userInfo.uuid forKey:@"appUserUuid"];
    
    UIButton *btn = (UIButton *)sender;
    switch (btn.tag) {
        case 0:
        {
            [para setObject:[_tF1 text] forKey:@"appUserName"];
            [[RequestManager getSingleton] sendRequest:updateUserName para:para callBack:^(id responseObj) {
                NSLog(@"aa%@",responseObj);
                if ([[responseObj objectForKey:@"status"] intValue] == 0) {
                    
                    [[DataCenter getSingleton] userInfo].userName= [_tF1 text];
                    [self.navigationController popToRootViewControllerAnimated:YES];
                }
            }];
            break;
        }
        case 1:
        {
            NSString *x =  [NSString stringWithFormat:@"%d",_segmentedControl.selectedSegmentIndex];
            [para setObject:x forKey:@"appUserGender"];//1：男 0：女
            [[RequestManager getSingleton] sendRequest:updateGender para:para callBack:^(id responseObj) {
                if ([[responseObj objectForKey:@"status"] intValue] == 0) {
                    [[DataCenter getSingleton] userInfo].sex= _segmentedControl.selectedSegmentIndex;
                    [self.navigationController popToRootViewControllerAnimated:YES];
                }
            }];
            break;
        }
        case 2:
        {
            if ([[_tF1 text] compare:@""] == 0 || [[_tF2 text] compare:@""] == 0 || [[_tF2 text] compare:@""] == 0)
            {
                return ;
            }
            if ([[_tF2 text] compare:@""] != [[_tF3 text] compare:@""]) {
                return ;
            }
            
            NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
            if ([[_tF1 text] compare:[standard objectForKey:@"UserPwd"]] != 0 ) {
                return ;
            }
            [para setObject:[_tF2 text] forKey:@"passWord"];
            [[RequestManager getSingleton] sendRequest:updatePassword para:para callBack:^(id responseObj) {
                if ([[responseObj objectForKey:@"status"] intValue] == 0) {
                    [[DataCenter getSingleton] userInfo].password= [_tF2 text];
                    [self.navigationController popToRootViewControllerAnimated:YES];

                }
            }];
            break;
        }
            
        default:
            break;
    }
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"信息设置"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"信息设置"];
}

@end
